package com.zz.ecommerce.declareManage.common.service;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.*;
import com.zz.ecommerce.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by admin on 2016/3/29.
 */
@Service
public class CancelBillService {

    @Autowired
    private ECancelBaseDao cancelBaseDao;

    @Autowired
    private ECancelConsumptionDao cancelConsumptionDao;

    @Autowired
    private ECancelEndgoodDao cancelEndgoodDao;

    @Autowired
    private ECancelMaterialsDao cancelMaterialsDao;

    @Autowired
    private ECancelDeclareDao cancelDeclareDao;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 查询所有核销数据
     *
     * @param request
     * @param base
     * @return
     */
    public GridResult queryBaseInfoList(GridRequest request, TECancelBase base) {
        int count = cancelBaseDao.count(TableConstants.T_E_CANCEL_BASE, base);
        List<TECancelBase> orderHeadList = cancelBaseDao.page(TableConstants.T_E_CANCEL_BASE, base,
                request.getStart(), request.getEnd(), request.getSortOrder());

        return new GridResult(orderHeadList, request, count);
    }

    /**
     * 删除核销单数据
     *
     * @param base
     * @return
     */
    public boolean deleteCancelBillInfo(TECancelBase base) {
        if (StringUtils.isEmpty(base.getId())) {
            logger.error("Delete failed, cancel-bill's id is empty!");
            return false;
        }

        String ids = "'" + base.getId().replace(",", "','") + "'";

        cancelBaseDao.delete(TableConstants.T_E_CANCEL_BASE, " WHERE ID IN( " + ids + ")");
        cancelDeclareDao.delete(TableConstants.T_E_CANCEL_DECLARE, " WHERE RELATE_ID IN(" + ids + ")");
        cancelConsumptionDao.delete(TableConstants.T_E_CANCEL_CONSUMPTION, " WHERE RELATE_ID IN(" + ids + ")");
        cancelEndgoodDao.delete(TableConstants.T_E_CANCEL_ENDGOOD, " WHERE RELATE_ID IN(" + ids + ")");
        cancelMaterialsDao.delete(TableConstants.T_E_CANCEL_MATERIALS, " WHERE RELATE_ID IN(" + ids + ")");

        return true;
    }

    /**
     * 查询核销单关联的所有单损耗数据
     *
     * @param request
     * @param relateId 关联订单唯一序列号
     * @return
     */
    public List<TECancelConsumption> queryAllConsumptionList(GridRequest request, String relateId) {
        TECancelConsumption consumption = new TECancelConsumption();
        consumption.setRelateId(relateId);
        request.setSidx("seqNo");
        request.setSord("asc");

        return cancelConsumptionDao.queryAll(TableConstants.T_E_CANCEL_CONSUMPTION, consumption,
                request.getSortOrder());
    }

    /**
     * 查询核销单关联的所有单损耗数据
     *
     * @param request
     * @param relateId 关联订单唯一序列号
     * @return
     */
    public List<TECancelDeclare> queryAllDeclareList(GridRequest request, String relateId) {
        TECancelDeclare declare = new TECancelDeclare();
        declare.setRelateId(relateId);
        request.setSidx("declDate");
        request.setSord("asc");

        return cancelDeclareDao.queryAll(TableConstants.T_E_CANCEL_DECLARE, declare,
                request.getSortOrder());
    }

    /**
     * 查询核销单关联的所有成品数据
     *
     * @param request
     * @param relateId 关联订单唯一序列号
     * @return
     */
    public List<TECancelEndgood> queryAllEndGoodsList(GridRequest request, String relateId) {
        TECancelEndgood endgood = new TECancelEndgood();
        endgood.setRelateId(relateId);
        request.setSidx("seqNo");
        request.setSord("asc");

        return cancelEndgoodDao.queryAll(TableConstants.T_E_CANCEL_ENDGOOD, endgood,
                request.getSortOrder());
    }

    /**
     * 查询核销单关联的所有成品数据
     *
     * @param request
     * @param relateId 关联订单唯一序列号
     * @return
     */
    public List<TECancelMaterials> queryAllMaterialsList(GridRequest request, String relateId) {
        TECancelMaterials materials = new TECancelMaterials();
        materials.setRelateId(relateId);
        request.setSidx("seqNo");
        request.setSord("asc");

        return cancelMaterialsDao.queryAll(TableConstants.T_E_CANCEL_MATERIALS, materials,
                request.getSortOrder());
    }


    public int getNextSeqNo(String tableName,String relateId){
        return cancelConsumptionDao.getNextSeqNo(tableName,relateId);
    }
}
